home *** CD-ROM | disk | FTP | other *** search
- From: RNorman@msn.com (Richard Norman)
- Subject: RE: Is This Bad Coding Practice?
- Date: 30 Mar 96 15:53:28 -0800
- References: <4jgnt2$9d1@loki.tor.hookup.net>
- Message-ID: <00001a80+00008b78@msn.com>
- Path: news.msn.com!msn.com
- Newsgroups: comp.lang.c
- Organization: The Microsoft Network (msn.com)
-
- R Singh asks:
- >char *func1(void)
- > {
- > char test[100];
- >
- > sprintf(test, "Test: %d", 1);
- > return test;
- > }
- >
-
- You don't ask if it usually works, but rather if it "bad" coding.
- Definitely. Once func1 returns, the array test no longer exists,
- so that your pointer is invalid.
-
- Your specific example uses the result immediately before anyone else
- gets a chance to contaminate the stack, which is probably where the
- array was stored. However, next week you will forget, and move
- part of your code into one function, the other part into another.
- Then, next month, you will be using a multitasking system where
- other threads have intervened between the function call and the
- use of the return value.
-
- Don't do it!
-